package com.management.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.management.entity.NewsComment;
import com.management.entity.NewsCommentExample;
import com.management.mapper.NewsCommentMapper;
import com.management.mapper.NewsDetailMapper;
import com.management.service.NewsCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author cd
 * @version 1.0
 * @date 2021/10/7 17:10
 */
@Service
public class NewsCommentServiceImpl implements NewsCommentService {
    NewsCommentMapper newsCommentMapper;
    NewsDetailMapper newsDetailMapper;

    @Autowired
    public void setNewsCommentMapper(NewsCommentMapper newsCommentMapper) {
        this.newsCommentMapper = newsCommentMapper;
    }

    @Autowired
    public void setNewsDetailMapper(NewsDetailMapper newsDetailMapper) {
        this.newsDetailMapper = newsDetailMapper;
    }


    @Override
    public PageInfo<NewsComment> getNewsCommentPageInfo(int pageNum, int pageSize, int newsDetailId) {
        // 开启分页
        PageHelper.startPage(pageNum, pageSize);
        // 开始查询
        NewsCommentExample newsCommentExample = new NewsCommentExample();
        NewsCommentExample.Criteria criteria = newsCommentExample.createCriteria();
        criteria.andNewsIdEqualTo(newsDetailId);

        // 查询
        List<NewsComment> newsComments = newsCommentMapper.selectByExample(newsCommentExample);
        PageInfo<NewsComment> pageInfo = new PageInfo<>(newsComments);

        return pageInfo;
    }

    @Override
    @Transactional
    public NewsComment addNewsComment(NewsComment newsComment) {

        int i = newsCommentMapper.insertSelective(newsComment);
        if (i == 0) {
            throw new RuntimeException("[ERROR] 添加失败");
        }
        return newsComment;
    }
}
